﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
using ZXing.PDF417;
using ZXing.PDF417.Internal;
using ZXing.QrCode;
using ZXing.QrCode.Internal;

namespace TestPDF417
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            BarcodeWriter writer = new BarcodeWriter();
            PDF417EncodingOptions options = new PDF417EncodingOptions();
            int h = 100;
            options = new PDF417EncodingOptions
            {
                Margin = Convert.ToInt32(txtMargin.Text),
                DisableECI = false,
                CharacterSet = cmbCharacterSet.SelectedItem.ToString(),
                ErrorCorrection = levels[cmbLevel.SelectedIndex],
                Width = h*Convert.ToInt32(txtLH.Text),
                Height =h 
            };
            //options.Hints.Add(EncodeHintType.CHARACTER_SET, cmbCharacterSet.SelectedItem.ToString());
            //该句灰常重要
            //options.Hints.Add(EncodeHintType.PDF417_COMPACTION, ZXing.PDF417.Internal.Compaction.BYTE);

            writer.Options = options;
            writer.Format = BarcodeFormat.PDF_417;
            Bitmap bitmap = writer.Write(txtResult.Text);

            pictureBoxQr.Image = bitmap;
        }
        /// <summary>
        /// 解密识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEnPDF417_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "所有文件|*.*";
            ofd.InitialDirectory = Directory.GetCurrentDirectory();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                BarcodeReader reader = new BarcodeReader();
                //PDF417Reader reader = new PDF417Reader();

                DecodingOptions options = new DecodingOptions();

                //QrCodeEncodingOptions qr = new QrCodeEncodingOptions();
                options.Hints.Add(DecodeHintType.CHARACTER_SET, cmbCharacterSet.SelectedItem.ToString());
                reader.Options = options;
                Result result = reader.Decode((Bitmap)Image.FromFile(ofd.FileName)); //通过reader解码

                if (result == null)
                {
                    MessageBox.Show("解析异常");
                }
                else
                {
                    txtResult.Text = result.Text;
                }
            }
        }
        /// <summary>
        /// 保存PDF417
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.InitialDirectory = Directory.GetCurrentDirectory();//当前程序目录
                sfd.FileName = "PDF417_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
                sfd.Filter = "图片|*.bmp";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    Bitmap bmp = new Bitmap(pictureBoxQr.Image);
                    bmp.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Bmp);//保存
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //校验等级
        PDF417ErrorCorrectionLevel[] levels = new PDF417ErrorCorrectionLevel[9];
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_Load(object sender, EventArgs e)
        {
            levels[0] = PDF417ErrorCorrectionLevel.L0;
            levels[1] = PDF417ErrorCorrectionLevel.L1;
            levels[2] = PDF417ErrorCorrectionLevel.L2;
            levels[3] = PDF417ErrorCorrectionLevel.L3;
            levels[4] = PDF417ErrorCorrectionLevel.L4;
            levels[5] = PDF417ErrorCorrectionLevel.L5;
            levels[6] = PDF417ErrorCorrectionLevel.L6;
            levels[7] = PDF417ErrorCorrectionLevel.L7;
            levels[8] = PDF417ErrorCorrectionLevel.L8;
            //显示校验等级到控件
            for (int i = 0; i < levels.Length; i++)
            {
                cmbLevel.Items.Add(levels[i].ToString());
            }
            cmbLevel.SelectedIndex = levels.Length-1;
            cmbCharacterSet.SelectedIndex = 0;
        }
    }
}
